package com.cmc.cloud.cmclink.doc.util.so;

import com.alibaba.fastjson.JSON;
import com.cmc.cloud.cmclink.common.filter.FilterGroup;
import com.cmc.cloud.cmclink.common.filter.FilterInfo;
import com.cmc.cloud.cmclink.common.filter.builder.expr.FilterExprBuilder;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.LimitRuleItemErrorCodeConstants;
import com.cmc.cloud.cmclink.expression.AviatorInstanceHolder;
import com.cmc.cloud.cmclink.expression.CmclinkExpression;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
 * PDF生成工具类
 *
 * @author chenzheng
 * @date 2025/08/01 11:25
 */

public class CmclinkExpressionUtil1 {

    static {
        AviatorInstanceHolder.initByScan();
    }

    public static void check() {
        String temp = "{\"filterType\":\"AND\",\"filterRules\":[{\"fieldName\":\"laneCode\",\"condition\":\"EQUAL\",\"fieldType\":\"STRING\",\"value\":\"133\"}],\"filterGroups\":[{\"filterType\":\"OR\",\"filterRules\":[{\"fieldName\":\"bkpCode\",\"condition\":\"EQUAL\",\"fieldType\":\"STRING\",\"value\":\"123\"}],\"filterGroups\":[{\"filterType\":\"AND\",\"filterRules\":[{\"fieldName\":\"bookingNo\",\"condition\":\"NOT_EQUAL\",\"fieldType\":\"STRING\",\"value\":\"123\"}],\"filterGroups\":[]}]}]}";
        String temp1 = "{\"filterType\":\"AND\",\"filterRules\":[{\"fieldName\":\"laneCode\",\"condition\":\"EQUAL\",\"fieldType\":\"STRING\",\"value\":\"133\"},{\"fieldName\":\"bookingNo\",\"condition\":\"NOT_EQUAL\",\"fieldType\":\"STRING\",\"value\":\"asd\"}],\"filterGroups\":[{\"filterType\":\"OR\",\"filterRules\":[{\"fieldName\":\"bkpCode\",\"condition\":\"EQUAL\",\"fieldType\":\"STRING\",\"value\":\"123\"}],\"filterGroups\":[{\"filterType\":\"AND\",\"filterRules\":[{\"fieldName\":\"bookingNo\",\"condition\":\"NOT_EQUAL\",\"fieldType\":\"STRING\",\"value\":\"123\"}],\"filterGroups\":[]}]}]}";
        String temp2 = "{\"filterType\":\"AND\",\"filterRules\":[{\"fieldName\":\"laneCode\",\"condition\":\"EQUAL\",\"fieldType\":\"STRING\",\"value\":\"1\"},{\"fieldName\":\"bookingNo\",\"condition\":\"NOT_EQUAL\",\"fieldType\":\"STRING\",\"value\":\"11\"}],\"filterGroups\":[{\"filterType\":\"OR\",\"filterRules\":[{\"fieldName\":\"bkpCode\",\"condition\":\"EQUAL\",\"fieldType\":\"STRING\",\"value\":\"2\"},{\"fieldName\":\"cntrOwner\",\"condition\":\"EQUAL\",\"fieldType\":\"STRING\",\"value\":\"21\"}],\"filterGroups\":[{\"filterType\":\"AND\",\"filterRules\":[{\"fieldName\":\"bookingNo\",\"condition\":\"NOT_EQUAL\",\"fieldType\":\"STRING\",\"value\":\"3\"},{\"fieldName\":\"voyageEx\",\"condition\":\"LESS_THAN\",\"fieldType\":\"STRING\",\"value\":\"31\"}],\"filterGroups\":[]}]}]}";

        String temp13 = "{\"filterType\":\"AND\",\"filterRules\":[{\"fieldName\":\"grossWeight\",\"condition\":\"BETWEEN\",\"fieldType\":\"STRING\",\"value\":\"100,150\"}],\"filterGroups\":[]}";
        String temp3 = "{\"filterType\":\"AND\",\"filterRules\":[{\"fieldName\":\"hsCode\",\"condition\":\"LIKE_RIGHT\",\"fieldType\":\"STRING\",\"value\":\"39219041,39219043\"}],\"filterGroups\":[]}";
        FilterGroup filterGroup = JSON.parseObject(temp3, FilterGroup.class);
        FilterInfo filterInfo = new FilterInfo();
        filterInfo.setFilterGroup(filterGroup);
        Map<String, Object> valueMap = new HashMap<>(16);
        valueMap.put("bookingParty", "CSM1923206070067679232");
        valueMap.put("bookingNo", "123");
        valueMap.put("cntrOwner", "SNL");
        valueMap.put("agreementNo", "234");
        valueMap.put("grossWeight", 120);
        valueMap.put("hsCode", "392190434");
        //规则转执行表达式
        Function<String, String> fieldNameConverter = new Function<String, String>() {
            public String apply(String s) {
                if (valueMap.get(s) != null) {
                    return "'" + valueMap.get(s) + "'";
                } else {
                    throw ServiceExceptionUtil.exception(LimitRuleItemErrorCodeConstants.LIMIT_RULE_ITEM_NOT_EXISTS);
                }
            }
        };
        String exeString = FilterExprBuilder.build(filterInfo);
        System.out.println(exeString);
        String exeString1 = FilterExprBuilderNew.build(filterInfo, fieldNameConverter);
        System.out.println(exeString1);
        CmclinkExpression exe = new CmclinkExpression(exeString1);
        System.out.println(exe.evaluate(valueMap));
        CmclinkExpression exe1 = new CmclinkExpression(exeString);
        boolean check = (boolean) exe1.evaluate(valueMap);
        System.out.println(check);
    }

    public static void main(String[] args) {
        CmclinkExpressionUtil1.check();
    }
}
